<?php
/**
 * SimpleRbacNodeController.php
 * Created by PhpStorm.
 * User: 江小波
 * Date: 2019/06/26
 * Time: 22:34
 */
namespace app\common\auto_curd\php_system\controller;

use app\common\auto_curd\php_system\model\SimpleRbacNodeModel;
use think\Controller;

abstract class SimpleRbacNodeController extends Controller
{
    /**
     * 创建操作
     * @return \think\response\View|void
     */
    public function create()
    {
        if($this->request->isPost()){
            $params = $this->request->post();
            $addData = [];

            // ID
            if(isset($params['id']) && $params['id']!="")
            {
                $addData['id'] = $params['id'];
            }

            // 分层
            if(isset($params['level']) && $params['level']!="")
            {
                $addData['level'] = $params['level'];
            }

            // 创建时间
            if(isset($params['create_ts']) && $params['create_ts']!="")
            {
                $addData['create_ts'] = $params['create_ts'];
            }

            // 修改时间
            if(isset($params['update_ts']) && $params['update_ts']!="")
            {
                $addData['update_ts'] = $params['update_ts'];
            }

            // 上级
            if(isset($params['pid']) && $params['pid']!="")
            {
                $addData['pid'] = $params['pid'];
            }

            // 顶级
            if(isset($params['tid']) && $params['tid']!="")
            {
                $addData['tid'] = $params['tid'];
            }

            // 排序
            if(isset($params['sort']) && $params['sort']!="")
            {
                $addData['sort'] = $params['sort'];
            }

            // 节点名
            if(isset($params['name']) && $params['name']!="")
            {
                $addData['name'] = $params['name'];
            }

            // 节点标题
            if(isset($params['title']) && $params['title']!="")
            {
                $addData['title'] = $params['title'];
            }

            // 树形描述
            if(isset($params['tree']) && $params['tree']!="")
            {
                $addData['tree'] = $params['tree'];
            }

            // 节点描述
            if(isset($params['remark']) && $params['remark']!="")
            {
                $addData['remark'] = $params['remark'];
            }

            $status = SimpleRbacNodeModel::save(null,$addData);
            if(!$status)
            {
                return $this->error('添加失败');
            }
            else
            {
                return $this->success('添加成功',url('update')."?id=".$status);
            }
        }else{
            return view('common/auto_curd/php_system@simple_rbac_node/create');
        }
    }
    
    /**
     * 更新操作
     */
    public function update()
    {
        if($this->request->isPost()){
            $params = $this->request->post();
            $saveData = [];

            // ID
            if(isset($params['id']) && $params['id']!="")
            {
                $saveData['id'] = $params['id'];
            }

            // 分层
            if(isset($params['level']) && $params['level']!="")
            {
                $saveData['level'] = $params['level'];
            }

            // 创建时间
            if(isset($params['create_ts']) && $params['create_ts']!="")
            {
                $saveData['create_ts'] = $params['create_ts'];
            }

            // 修改时间
            if(isset($params['update_ts']) && $params['update_ts']!="")
            {
                $saveData['update_ts'] = $params['update_ts'];
            }

            // 上级
            if(isset($params['pid']) && $params['pid']!="")
            {
                $saveData['pid'] = $params['pid'];
            }

            // 顶级
            if(isset($params['tid']) && $params['tid']!="")
            {
                $saveData['tid'] = $params['tid'];
            }

            // 排序
            if(isset($params['sort']) && $params['sort']!="")
            {
                $saveData['sort'] = $params['sort'];
            }

            // 节点名
            if(isset($params['name']) && $params['name']!="")
            {
                $saveData['name'] = $params['name'];
            }

            // 节点标题
            if(isset($params['title']) && $params['title']!="")
            {
                $saveData['title'] = $params['title'];
            }

            // 树形描述
            if(isset($params['tree']) && $params['tree']!="")
            {
                $saveData['tree'] = $params['tree'];
            }

            // 节点描述
            if(isset($params['remark']) && $params['remark']!="")
            {
                $saveData['remark'] = $params['remark'];
            }

            $status = SimpleRbacNodeModel::save($saveData['id'],$saveData);
            if(!$status)
            {
                return $this->error('更新失败');
            }
            else
            {
                return $this->success('更新成功');
            }
        }else{
            $row = SimpleRbacNodeModel::get(['id'=>$this->request->get('id')]);
            return view('common/auto_curd/php_system@simple_rbac_node/update',[
                'row' => $row
            ]);
        }
    }
    /**
     * 获取操作
     */
    public function read()
    {

        $list = SimpleRbacNodeModel::paginate([]);
        $pagination = $list->render();
        return view('common/auto_curd/php_system@simple_rbac_node/read',[
            'list' => $list,
            'pagination' => $pagination
        ]);

    }
    /**
     * 删除操作
     */
    public function delete()
    {

        $status = SimpleRbacNodeModel::delete($this->request->param('id'));
        if(!$status)
        {
            return $this->error('删除失败');
        }
        else
        {
            return $this->success('删除成功');
        }

    }
    
}